<template>
  <div>
    <h1>毕业答辩</h1>

    <!-- ----------------------------------------------------------- -->
    <el-descriptions>
      <el-descriptions-item label="小组类型">
        {{ groupTeacher.group_style }}
      </el-descriptions-item>
      <el-descriptions-item label="组号">
        {{ groupTeacher.group_num }}
      </el-descriptions-item>
      <el-descriptions-item label="是否组长">
        {{ groupTeacher.group_leader }}
      </el-descriptions-item>
      <el-descriptions-item label="答辩地点">
        {{ groupTeacher.group_address }}
      </el-descriptions-item>
      <el-descriptions-item label="答辩时间">
        {{ groupTeacher.time | formatData }}
      </el-descriptions-item>
    </el-descriptions>
    <!-- ---------------------------------------------------------------- -->

    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="姓名" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.student_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="论文" width="180">
        <template slot-scope="scope">
          <a href="javascript:;" @click="download(scope.row)">{{
            scope.row.article_name
          }}</a>
        </template>
      </el-table-column>
      <el-table-column label="答辩状态" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.pass_status }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">评分</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹出框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item prop="name" label="评分">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item prop="desc" label="评语">
          <el-input type="textarea" v-model="form.desc"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="truescore()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  created() {
    this.group_student();
    this.group_teacher();
  },
  data() {
    return {
      n: "",
      groupTeacher: {},
      form: {},
      dialogVisible: false,
      tableData: [],
      rules: {
        name: [{ required: true, message: "必填", trigger: "blur" }],
        desc: [{ required: true, message: "必填", trigger: "blur" }],
      },
    };
  },
  filters: {
    formatData(time) {
      return new Date(time).toLocaleString();
    },
  },
  methods: {
    handleEdit(row) {
      if (this.groupTeacher.group_leader == "否") {
        this.dialogVisible = false;
        this.$message.error("你不是组长");
      } else {
        this.dialogVisible = true;
        this.n = row;
      }
      // console.log(row);
    },

    // 下载论文
    download(row) {
      // console.log(row);
      location.href =
        "http://127.0.0.1:8080/api/teacher_download?id=" + row.article_id;
    },

    // 确认评分按钮
    truescore() {
      const obj = { ...this.n, ...this.form };
      console.log(obj);
      this.$refs.form.validate((valid) => {
        if (!valid) return;
        this.$http
          .post(
            "http://127.0.0.1:8080/teacher/group_score",
            this.$qs.stringify(obj)
          )
          .then((res) => {
            if (res.data.status == 0) {
              this.dialogVisible = false;
              this.$message.success(res.data.message);
              this.form = {};
              this.group_student();
            } else {
              this.$message.error(res.data.message);
            }
          })
          .catch((err) => {
            this.$message.error(err);
          });
      });
    },

    // 获取分配到的学生
    group_student() {
      this.$http
        .get("http://127.0.0.1:8080/teacher/graduation_group")
        .then((res) => {
          if (res.data.status == 0) {
            this.tableData = res.data.data;
          }
          // else {
          //   this.$message.error(res.data.message);
          // }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },

    //获取当前教师的分组情况
    group_teacher() {
      this.$http
        .get("http://127.0.0.1:8080/teacher/group_teacherinfo2")
        .then((res) => {
          // console.log(res);
          if (res.data.status == 0) {
            this.groupTeacher = res.data.data[0];
          }
          // else {
          //   this.$message.error(res.data.message);
          // }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
  },
};
</script>
<style lang="less" scoped>
.el-descriptions {
  margin: 20px 0;
}
</style>
